#!/bin/bash

# Copyright (c) Calibra. All Rights Reserved

# run this script from the project root using `./scripts/build_docs.sh`

usage() {
  echo "Usage: $0 [-b]"
  echo ""
  echo "Build Libra documentation."
  echo ""
  echo "  -b   Build static version of documentation (otherwise start server)"
  echo ""
  exit 1
}

BUILD_STATIC=false

while getopts 'hb' flag; do
  case "${flag}" in
    h)
      usage
      ;;
    b)
      BUILD_STATIC=true
      ;;
    *)
      usage
      ;;
  esac
done

echo "-----------------------------------"
echo "Generating API reference via Rustdoc"
echo "-----------------------------------"

# Clone and Build Libra Core docs
#cd ../..
#git clone git@github.com:libra/libra.git libracore
#cd libracore/libra
#git pull
#cargo doc --no-deps
#cd ../../website

# manually copy crate README files from fixed directory
###

RUSTDOC_DIR='../libra/target/doc/'
DOCUSAURUS_RUSTDOC_DIR='./website/static/rustdoc/'

# mkdir -p $DOCUSAURUS_RUSTDOC_DIR
# echo $RUSTDOC_DIR $DOCUSAURUS_RUSTDOC_DIR
# move JS/CSS design assets
# echo "cp -r $RUSTDOC_DIR $DOCUSAURUS_RUSTDOC_DIR"

# cd libra || exit
# cargo doc
# cd .. || exit

echo "-----------------------------------"
echo "Building Docusaurus"
echo "-----------------------------------"
cd website || exit
yarn

# run script to parse html generated by rustdoc
echo "-----------------------------------"
echo "Parsing Rustdoc and Protogen docs and moving to Docusaurus"
echo "-----------------------------------"
cd ..
# mkdir -p "website/pages/api/"


cd website || exit

if [[ $BUILD_STATIC == true ]]; then
  echo "-----------------------------------"
  echo "Building static site"
  echo "-----------------------------------"
  yarn build
else
  echo "-----------------------------------"
  echo "Starting local server"
  echo "-----------------------------------"
  yarn start
fi
